package com.wk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wk.dao.*;
import com.wk.entity.*;
import com.wk.entity.CmfzResource;
import com.wk.service.CmfzShiroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CmfzShiroServiceImpl implements CmfzShiroService {


    @Autowired
    private CmfzAdminDao cmfzAdminDao;

    @Autowired
    private CmfzAdminRoleDao cmfzAdminRoleDao;


    @Autowired
    private CmfzRoleDao cmfzRoleDao;

    @Autowired
    private CmfzRoleResourceDao cmfzRoleResourceDao;

    @Autowired
    private CmfzResourceDao cmfzResourceDao;

    @Override
    public Set<String> selectRoleNameByUsername(String username) {

        CmfzAdmin cmfzAdmin = cmfzAdminDao.selectOne(new QueryWrapper<CmfzAdmin>().eq("username",username));

        List<CmfzAdminRole> list = cmfzAdminRoleDao.selectList(new QueryWrapper<CmfzAdminRole>().eq("admin_id", cmfzAdmin.getId()));

        Set set = new HashSet();

        for (CmfzAdminRole cmfzAdminRole : list) {
            System.out.println("adminRole"+cmfzAdminRole);
            CmfzRole cmfzRole = cmfzRoleDao.selectById(cmfzAdminRole.getRoleId());
            set.add(cmfzRole.getRoleName());
        }


        return set;
    }


//    @Override
//    public Set<String> selectPermissionByUsername(String username) {
//        Set hashSet = new HashSet();
//        //先查用户
//        CmfzAdmin cmfzAdmin1 = cmfzAdminDao.selectOne(new QueryWrapper<CmfzAdmin>().eq("username", username));
//
//        //查角色用户表
//        List<CmfzAdminRoleService> list = cmfzAdminRoleDao.selectList(new QueryWrapper<CmfzAdminRoleService>().eq("admin_id", cmfzAdmin1.getId()));
//
//        for (CmfzAdminRoleService cmfzAdminRole : list) {
//            //通过角色ID查权限角色表
//            List<CmfzRolePer> list1 = cmfzRolePerDao.selectList(new QueryWrapper<CmfzRolePer>().eq("role_id", cmfzAdminRole.getRoleId()));
//            for (CmfzRolePer cmfzRolePer : list1) {
//                //通过权限Id查每一个
//                CmfzPer role_id1 = cmfzPerDao.selectById( cmfzRolePer.getPerId());
//                hashSet.add(role_id1.getPermission());
//            }
//        }
//
//        return hashSet;
//    }

    public Set<String> selectPermissionByUsername(String username) {
        HashSet<String> permissions = new HashSet<>();
        Set<String> allRolesByUsername = selectRoleNameByUsername(username);


        for (String roleName : allRolesByUsername) {
//            根据角色名 查询 角色权限表 得到权限Id
            List<CmfzRoleResource> roleResources = cmfzRoleResourceDao.selectList(new QueryWrapper<CmfzRoleResource>().eq("role_name", roleName));

            for (CmfzRoleResource roleResource : roleResources) {

//                根据权限Id 查询权限表得到权限字符串  where resource_type=function

                CmfzResource cmfzResource =cmfzResourceDao.selectOne(new QueryWrapper<CmfzResource>().eq("resource_id",roleResource.getResourceId()).eq("resource_type","function"));                if (cmfzResource != null) {
                    permissions.add(cmfzResource.getResourcePermission());
                }
            }
        }

        return permissions;
    }
}
